package com.ruoyi.blog.web;

import com.ruoyi.blog.dto.response.Result;
import com.ruoyi.blog.util.EmailUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("")
public class MailController {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Autowired
    private TaskExecutor taskExecutor;

    @GetMapping("/send-to-email")
    public String sendToEmail(String email) {
        String code = RandomStringUtils.randomNumeric(6);
        String[] to = {email};// 收件人邮箱，支持传
        taskExecutor.execute(() -> {
            EmailUtil.sendNoFromMail(to, "欢迎注册XX论坛", "注册验证码是：" + code, new ArrayList<>(), javaMailSender);
        });
        Map<String, Object> result = new HashMap<>();
        result.put("msg", "邮箱验证码发送成功");
        return "user/reg";
    }

    @GetMapping("/sendto163")
    public Result sendto163() {
        String from = "1135365441@qq.com";// 发件人邮箱
        String[] to = {"18747633468@163.com"};// 收件人邮箱，支持传
        String password = "tynmdlqdelfpjiha";
        String host = "smtp.qq.com";
        List<File> files = new ArrayList<>();
        File file = new File("/Users/lizhenghang/Desktop/网上购物系统设计与实现.docx");
        files.add(file);
        taskExecutor.execute(() -> {
            EmailUtil.sendMail(from, to, host, password, "测试标题11111 to 163", "测试内容11111 to 163", files);
        });
        return Result.ok("发送成功！");
    }

}
